package cn.tedu.oa.user.controller;

import cn.tedu.oa.base.response.JsonResult;
import cn.tedu.oa.user.pojo.dto.UserLoginParam;
import cn.tedu.oa.user.pojo.dto.UserLogoutParam;
import cn.tedu.oa.user.pojo.dto.UserQuery;
import cn.tedu.oa.user.pojo.dto.UserSaveParam;
import cn.tedu.oa.user.pojo.entity.User;
import cn.tedu.oa.user.pojo.vo.UserVO;
import cn.tedu.oa.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/oa1/user")
public class UserController {
    //Controller需要注入Service层,才可以调用业务层的方法!
    @Autowired
    UserService userService;//注入的是接口!!!

    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
        log.debug("用户登录:参数={}",userLoginParam);
        //调用业务层的login方法
        //Ctrl去接口  Ctrl+Alt去接口实现类  Ctrl+Shift去方法返回值类
        UserVO userVO = userService.login(userLoginParam);
        //将业务层返回的userVO对象封装到JsonResult中返回给前端
        return JsonResult.ok(userVO);
    }

    @PostMapping("save")
    public JsonResult saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户:参数={}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户:参数={}",userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        log.debug("查询用户:结果={}",list);
        return JsonResult.ok(list);
    }

    @PostMapping("/update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Integer userId){
        log.debug("重置密码:参数={}",userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @PostMapping("/update/status/{userId}/{status}")
    public JsonResult updateStatus(
            @PathVariable Integer userId,@PathVariable String status){
        log.debug("修改用户状态:参数={},{}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{userId}")
    public JsonResult deleteUser(@PathVariable Integer userId){
        log.debug("删除用户:参数={}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    @GetMapping("/select/audit/{parentId}")
    public JsonResult selectAuditList(@PathVariable Integer parentId){
        log.debug("查询审批人列表:参数={}",parentId);
        List<UserVO> list = userService.selectAuditList(parentId);
        return JsonResult.ok(list);
    }
    @GetMapping("/selectlevel")
    public JsonResult selectleader(UserQuery userQuery){
        log.debug("查询领导:参数={}",userQuery);
        List<UserVO> list = userService.selectLeader(userQuery);
        log.debug("查询领导:结果={}",list);
        return JsonResult.ok(list);
    }
    @PostMapping("onlineDuration")
    public JsonResult upOnlineDuration(UserLogoutParam userLogoutParam){
        log.debug("更新在线时长:参数={}",userLogoutParam);
        userService.updateLogoutTime(userLogoutParam);
        return JsonResult.ok();
    }
    @GetMapping("selectByDuration")
    public JsonResult selectByDuration(){
        log.debug("查询活跃员工:参数={}");
        List<UserVO> list=userService.selectByDuration();
        return JsonResult.ok(list);
    }
    @GetMapping("selectCount")
    public JsonResult selectCount(){
        log.debug("查询员工数量:参数={}");
        Integer count=userService.selectCount();
        return JsonResult.ok(count);
    }


}
